//
//  HomeViewController.swift
//  ExtremeHealth
//
//  Created by Zhongkai Li 李忠凯 on 2018/8/22.
//  Copyright © 2018 GanDongKeJi. All rights reserved.
//

import Eureka

class EHBaseFormViewController: FormViewController {
    let section = Section()

    init() {
        super.init(style: .plain)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }

        form.append(section)
        setupRows()

        form.allRows.forEach { (row) in
            row.baseCell.selectionStyle = .none
        }

        tableView.separatorStyle = .none
        tableView.allowsMultipleSelection = false
        tableView.allowsSelectionDuringEditing = false
        tableView.allowsMultipleSelectionDuringEditing = false
    }

    func setupRows() { fatalError("subclass override") }

    func addRow(_ row: BaseRow) { section.append(row) }

    func addSeperatorRow(gapHeight: CGFloat) {
        let row = EHTitleRow { row in
            row.baseCell.height = { gapHeight }
        }
        addRow(row)
    }

    func addLineRow(leftMargin: CGFloat = 80, rightMargin: CGFloat = 80) {
        let row = EHSeperatorLineRow { row in
            row.leftMargin = leftMargin
            row.rightMargin = rightMargin
        }
        addRow(row)
    }

    func addSeperatorLineRow(withMargins edgeInsets: UIEdgeInsets) {
        addSeperatorRow(gapHeight: edgeInsets.top)
        addLineRow(leftMargin: edgeInsets.left, rightMargin: edgeInsets.right)
        addSeperatorRow(gapHeight: edgeInsets.bottom)
    }
}
